package test.yzx.pproject.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HomeSingleNumberView extends LinearLayout {

    private final int childCount = 10;
    private int textColor = Color.parseColor("#616161");
    private int textSize = 54;

    public HomeSingleNumberView(Context context) {
        this(context, null);
    }
    public HomeSingleNumberView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init(){
        setOrientation(VERTICAL);
        for (int i = 0; i < childCount; i++)
            addView(initTextView(new TextView(getContext())));
        initNumberText();
    }
    private TextView initTextView(TextView child){
        child.setGravity(Gravity.CENTER);
        child.setTextColor(textColor);
        child.setTextSize(textSize);
        return child;
    }
    /* 最上是9, 最底是0 */
    private void initNumberText(){
        for (int i = 0; i < childCount; i++)
            ((TextView)getChildAt(i)).setText(9 - i+"");
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
        int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
        for (int i = 0; i < childCount; i++)
            getChildAt(i).measure(widthSpec, heightSpec);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        for (int i = 0; i < childCount; i++)
            getChildAt(i).layout(0, i*getHeight(), getWidth(), (i+1)*getHeight());
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        endAnim();
    }



    /* end anim */
    private void endAnim(){
        if(mAnim!=null)
            mAnim.end();
        mAnim = null;
    }
    /* get scroll Y position by number */
    private int getScrollYByNumber(int number){
        return getHeight() * (9-number);
    }
    /* start scroll anim to target number, relative current scrollY position */
    private void startAnim(int duration){
        endAnim();
        mAnim = ValueAnimator.ofInt(getScrollY(), getScrollYByNumber(currentNumber));
        mAnim.setDuration(duration);
        mAnim.setInterpolator(mInterpolator);
        mAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                scrollTo(0, (int)animation.getAnimatedValue());
            }
        });
        mAnim.addListener(new AnimatorListenerAdapter(){
            public void onAnimationEnd(Animator animation) {
                mAnim=null;
            }
        });
        mAnim.start();
    }



    private Interpolator mInterpolator = new DecelerateInterpolator(1.6f);
    private ValueAnimator mAnim;
    //default 9
    private int currentNumber = 9;


    /** 提供插值器 */
    public void offerInterpolator(Interpolator interpolator){
        mInterpolator = interpolator;
    }
    /** 设置显示的数字 */
    public void setNumber(int number){
        endAnim();
        this.currentNumber = number;
        scrollTo(0, getScrollYByNumber(currentNumber));
    }
    /** get current number */
    public int getCurrentNumber(){
        return currentNumber;
    }
    /**  */
    public void setTextColor(@ColorInt int color){
        this.textColor = color;
        for (int i = 0; i < childCount; i++)
            ((TextView)getChildAt(i)).setTextColor(color);
    }
    /**  */
    public void setTextSize(int dp){
        this.textSize = dp;
        for (int i = 0; i < childCount; i++)
            ((TextView)getChildAt(i)).setTextSize(dp);
    }
    /** anim to target number */
    public void animTo(@IntRange(from = 0, to = 9) int number, final int duration){
        if(number <0 || number>9)
            throw new IllegalStateException("number must between 0 and 9");
        this.currentNumber = number;
        post(new Runnable() {
            public void run() {
                startAnim(duration);
            }
        });
    }

}
